<template>
  <!-- 财务审核 -->
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="支付日期" prop="payDate">
        <el-date-picker
          v-model="payDate"
          type="daterange"
          align="right"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :picker-options="pickerOptions"
          value-format="yyyy-MM-dd"
        >
        </el-date-picker>
      </el-form-item>

      <el-form-item label="支付方式" prop="payType">
        <el-select
          v-model="queryParams.payType"
          placeholder="支付方式"
          clearable
          size="small"
          style="width: 150px"
        >
          <el-option
            v-for="dict in payList"
            :key="dict.code"
            :label="dict.msg"
            :value="dict.code"
          />
        </el-select>
      </el-form-item>

      <!-- <el-form-item label="入账状态" prop="entryStatus">
        <el-select
          v-model="queryParams.entryStatus"
          placeholder="入账状态"
          clearable
          size="small"
          style="width: 150px"
        >
          <el-option
            v-for="dict in orderRu"
            :key="dict.code"
            :label="dict.msg"
            :value="dict.code"
          />
        </el-select>
      </el-form-item> -->

      <el-form-item label="订单状态" prop="orderStatus">
        <el-select
          v-model="queryParams.orderStatus"
          placeholder="订单状态"
          clearable
          size="small"
          style="width: 150px"
        >
          <el-option
            v-for="dict in statusList"
            :key="dict.code"
            :label="dict.msg"
            :value="dict.code"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="核账状态" prop="checkStatus">
        <el-select
          v-model="queryParams.checkStatus"
          placeholder="核账状态"
          clearable
          size="small"
          style="width: 150px"
        >
          <el-option
            v-for="dict in orderHe"
            :key="dict.code"
            :label="dict.msg"
            :value="dict.code"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="车站" prop="departureStation">
        <el-autocomplete
          class="inline-input"
          clearable
          v-model="queryParams.departureStation"
          :fetch-suggestions="StationQuerySearch"
          placeholder="请输入内容"
          :trigger-on-focus="false"
        ></el-autocomplete>
      </el-form-item>
      <!-- <el-input
          v-model="queryParams.departureStation"
          placeholder="请输入车站"
          clearable
          style="width: 230px"
          size="small"
          @keyup.enter.native="handleQuery"
        /> -->
      <div>
        <el-form-item label="发车日期" prop="StartDate">
          <el-date-picker
            v-model="StartDate"
            type="daterange"
            align="right"
            unlink-panels
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :picker-options="pickerOptions"
            value-format="yyyy-MM-dd"
            @change="getPreDateArr"
          >
          </el-date-picker>
        </el-form-item>
        <!-- <el-form-item label="保险商" prop="baoxian">
          <el-select
            v-model="queryParams.baoxian"
            placeholder="保险商"
            clearable
            size="small"
            style="width: 110px"
          >
            <el-option
              v-for="dict in orderTypeList"
              :key="dict.code"
              :label="dict.msg"
              :value="dict.code"
            />
          </el-select>
        </el-form-item> -->
        <el-form-item label="订单号" prop="keyword">
          <el-autocomplete
            style="width: 300px"
            class="inline-input"
            clearable
            v-model="keyword"
            :fetch-suggestions="querySearch"
            placeholder="请输入订单号、流水号搜索"
            @select="handleSelect"
            @keyup.enter.native="handleQuery"
          ></el-autocomplete>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            v-hasPermi="['finance:Account:query']"
            >搜索</el-button
          >
          <el-button
            type="primary"
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['finance:Account:export']"
            >导出</el-button
          >
        </el-form-item>
      </div>
    </el-form>
    <!-- 详情 -->
    <el-dialog title="详情" :visible.sync="dialogVisible" width="890px">
      <div id="msgBody">
        <div class="msgTitle" style="width: 100px">
          <p>流水单号</p>
          <p>订单编号</p>
          <p>票价</p>
          <p>支付时间</p>
        </div>
        <div class="msgCont" style="width: 260px">
          <p>{{ message.transactionId ? message.transactionId : "无" }}</p>
          <p>{{ message.outTradeNo ? message.outTradeNo : "无" }}</p>
          <p>{{ message.ticketPrice ? message.ticketPrice + "元" : "无" }}</p>
          <p>
            {{ newDate(message.orderTime) ? newDate(message.orderTime) : "无" }}
          </p>
        </div>
        <div class="msgTitle" style="width: 100px">
          <p>支付方式</p>
          <p>订单状态</p>
          <p>服务费</p>
          <p>红包</p>
        </div>
        <div class="msgCont" style="width: 110px">
          <p>{{ message.payType == 1 ? "微信" : "支付宝" }}</p>
          <p>
            {{
              getStatus(message.orderStatus)
                ? getStatus(message.orderStatus)
                : "无"
            }}
          </p>
          <p>{{ message.servicePrice + "元" }}</p>
          <p>{{ message.redEnvelopes + "元" }}</p>
        </div>
        <div class="msgTitle" style="width: 100px">
          <p>支付状态</p>
          <p>保险</p>
          <p>数量</p>
          <p>实付金额</p>
        </div>
        <div class="msgCont" style="width: 110px">
          <p>成功</p>
          <p>{{ message.insurancePremium }}元</p>
          <p>{{ message.zzs ? message.zzs : 0 }}</p>
          <p>{{ message.settlementTotalFee + "元" }}</p>
        </div>
      </div>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>

    <el-table
      v-loading="loading"
      :data="list"
      @selection-change="handleSelectionChange"
    >
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="序号" align="center" type="index" width="50" />
      <el-table-column
        label="支付流水单号"
        align="center"
        prop="transactionId"
        width="260"
      />
      <el-table-column label="支付方式" align="center" prop="payType">
        <template slot-scope="scope">
          <span>{{ scope.row.payType == 1 ? "微信" : "支付宝" }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="入账状态" align="center" prop="entryStatus">
        <template slot-scope="scope">
          <span>{{ scope.row.entryStatus == 1 ? "成功" : "失败" }}</span>
        </template>
      </el-table-column> -->
      <el-table-column
        label="订单编号"
        align="center"
        prop="outTradeNo"
        width="200"
      />
      <el-table-column label="订单状态" align="center" width="100">
        <template slot-scope="scope">
          <span>{{ getStatus(scope.row.orderStatus) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="出发车站"
        align="center"
        prop="departureStation"
        width="200"
      />
      <el-table-column
        label="支付时间"
        align="center"
        prop="orderTime"
        width="160"
      >
        <template slot-scope="scope">
          <span>{{ newDate(scope.row.orderTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发车时间" align="center" width="150">
        <template slot-scope="scope">
          <span>{{ NewDateYear(scope.row.departureTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="实付金额"
        align="center"
        prop="settlementTotalFee"
        width="100"
      />
      <el-table-column label="核账状态" align="center" prop="checkStatus">
        <template slot-scope="scope">
          <span
            :style="{
              color:
                scope.row.checkStatus == '0'
                  ? ''
                  : scope.row.checkStatus == '1'
                  ? '#2A8E34'
                  : 'red',
            }"
            >{{
              scope.row.checkStatus == "0"
                ? "未核账"
                : scope.row.checkStatus == "1"
                ? "正常"
                : "异常"
            }}</span
          >
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            size="mini"
            @click="toMessage(scope.row.id)"
            v-hasPermi="['finance:Account:query']"
            >详情</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { applyrefund } from "@/api/service/orderList";
import { list, Account } from "@/api/finance/credit";
import { querystations } from "@/api/order/refundList";
// import { auditList } from "@/api/finance/audit";
import utils from "../../js/index";

export default {
  name: "Credit",
  components: {},
  data() {
    return {
      orderTypeList: [{ msg: "全部", code: "" }],
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      orderRu: [
        { msg: "全部", code: "" },
        { msg: "成功", code: "1" },
        { msg: "失败", code: "2" },
      ],
      orderHe: [
        { msg: "全部", code: "" },
        // { msg: "未核账", code: "0" },
        { msg: "正常", code: "1" },
        { msg: "异常", code: "2" },
      ],
      statusList: [
        { msg: "全部", code: "" },
        { msg: "待支付", code: 0 },
        { msg: "已支付", code: 1 },
        { msg: "已取消", code: 2 },
        { msg: "已打印", code: 3 },
        { msg: "申请退款", code: 6 },
        { msg: "退款成功", code: 7 },
        { msg: "用户申请退款", code: 8 },
        { msg: "用户申请退款成功", code: 9 },
      ],
      payList: [
        { msg: "全部", code: "" },
        { msg: "微信", code: "1" },
        { msg: "支付宝", code: "2" },
      ],
      dialogVisible: false,
      message: {},
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderType: 1,
        payType: "",
        entryStatus: "",
        orderStatus: "",
        departureStation: "",
        checkStatus: "",
        outTradeNo: "",
        transactionId: "",
        beginDepartureTime: "",
        endDepartureTime: "",
        baoxian: "",
        billingStatus: "",
      },
      payDate: [],
      StartDate: [],
      keyword: "",
      arr: [],
      stationList: [],
    };
  },
  created() {
    this.getList();
    this.getStationList();
  },

  methods: {
    StationQuerySearch(queryString, cb) {
      var restaurantsStation = this.stationList;
      var results = queryString
        ? restaurantsStation.filter(this.StationCreateFilter(queryString))
        : restaurantsStation;
      // 调用 callback 返回建议列表的数据
      cb(results);
    },
    StationCreateFilter(queryString) {
      return (v) => {
        return v.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0;
      };
    },
    getStationList() {
      querystations().then((res) => {
        if (res.code == 200) {
          var arr = [];
          res.data.forEach((item) => {
            arr.push({ value: item.stationName });
          });
          this.stationList = arr;
        } else {
          this.$message.error(res.msg);
        }
      });
    },
    toMessage(code) {
      Account(code)
        .then((res) => {
          if (res.code == 200) {
            this.dialogVisible = true;
            this.message = res.data;
          } else {
            this.$message.success(res.msg);
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    getPreDateArr(date) {
      if (!date) {
        this.queryParams.beginDepartureTime = "";
        this.queryParams.endDepartureTime = "";
        return;
      }
      this.queryParams.beginDepartureTime = date[0];
      this.queryParams.endDepartureTime = date[1];
    },
    getStatus(val) {
      switch (val) {
        case 0:
          return "待支付";
          break;
        case 1:
          return "已支付";
          break;
        case 2:
          return "已取消";
          break;
        case 3:
          return "已打印";
          break;
        case 6:
          return "申请退款";
          break;
        case 7:
          return "退款成功";
          break;
        case 8:
          return "用户申请退款";
          break;
        case 9:
          return "用户申请退款成功";
          break;

        default:
          break;
      }
    },
    handleSelect(item) {},
    querySearch(queryString, cb) {
      try {
        var restaurants = this.arr;
        var results = queryString
          ? restaurants.filter(this.createFilter(queryString))
          : restaurants;
        // 调用 callback 返回建议列表的数据
        cb(results);
      } catch (err) {}
    },
    createFilter(queryString) {
      return (restaurant) => {
        return (
          restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) ===
          0
        );
      };
    },
    /** 查询列表 */
    getList() {
      const that = this;
      this.loading = true;
      list(this.addDateRange(this.queryParams, this.payDate))
        .then((response) => {
          if (response.code == 200) {
            this.list = response.rows;
            this.total = response.total;
            this.loading = false;
          }
        })
        .catch((err) => {
          this.list = [];

          this.loading = false;
        });
    },
    newDate(date) {
      return utils.utils.NewDate(date);
    },
    NewDateYear(date) {
      return utils.utils.NewDateYear(date);
    },
    installParams() {
      if (this.keyword.length == 16) {
        this.queryParams.outTradeNo = this.keyword;
        this.queryParams.transactionId = "";
      } else {
        this.queryParams.transactionId = this.keyword;
        this.queryParams.outTradeNo = "";
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.installParams();
      this.getList();
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.infoId);
      this.multiple = !selection.length;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const infoIds = row.infoId || this.ids;
      this.$confirm(
        '是否确认删除访问编号为"' + infoIds + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delLogininfor(infoIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.installParams();
      this.queryParams.pageNum = 0;
      this.queryParams.pageSize = 0;
      const loading = this.$loading({
        lock: true,
        text: "数据量过多，正在努力加载...",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)",
      });
      this.download(
        "/jxbill/Account/export",
        {
          ...this.queryParams,
        },
        `财务入账订单_${utils.utils.NewDate()}.xlsx`
      ).then((res) => {
        loading.close();
      });
      this.queryParams.pageNum = 1;
      this.queryParams.pageSize = 10;
    },
  },
};
</script>

<style scoped>
.refItem {
  line-height: 50px;
}
#refDiv {
  font-size: 15px;
}
#msgBody {
  text-align: center;
}

.msgItemCont {
  width: 120px;
  line-height: 40px;
  display: inline-block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.msgTitle {
  width: 130px;
  display: inline-block;
  text-align: center;
  background: #f2f2f2;
}
.msgTitle p {
  line-height: 40px;
  border-bottom: 1px solid #d7d7d7;
  border-right: 1px solid #d7d7d7;
  border-left: 1px solid #d7d7d7;
  padding: 0;
  margin: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.msgTitle p:nth-child(1),
.msgCont p:nth-child(1) {
  border-top: 1px solid #d7d7d7;
}
.msgCont {
  display: inline-block;
  text-align: center;
}
#msgBody .msgCont:last-child {
  border-right: 1px solid #d7d7d7;
}
.msgCont p {
  line-height: 40px;
  border-bottom: 1px solid #d7d7d7;
  padding: 0;
  margin: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
</style>